<template>
  <div class="goodsdoc-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button
          :icon="DocumentAdd"
          plain
          type="primary"
          @click="openAddDialog"
        >
          新增
        </el-button>
        <el-button
          :disabled="!currentXz"
          :icon="Edit"
          plain
          type="warning"
          @click="openUpdateDialog"
        >
          修改
        </el-button>
        <el-button :icon="Delete" plain type="primary" @click="deleteWc">
          删除
        </el-button>
        <el-button :icon="Printer" plain type="primary" @click="toPrint">
          打印
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '6px 6px' }"
      :data="goods"
      highlight-current-row
      @current-change="handleCurrentChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="45" />
      <el-table-column label="货位号" prop="goodsShelvesCode" width="200" />
      <el-table-column label="货位名称" prop="goodsShelvesName" width="200" />
      <el-table-column label="货位类型" width="150">
        <template #default="scope">
          <el-tag
            v-if="scope.row.goodsShelvesType === 0"
            effect="dark"
            type="primary"
          >
            货架
          </el-tag>
          <el-tag
            v-else-if="scope.row.goodsShelvesType === 1"
            effect="dark"
            type="success"
          >
            地面
          </el-tag>
          <el-tag v-else effect="dark" type="warning">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createUser" width="150" />
      <el-table-column label="创建时间" prop="createTime" width="250" />
      <el-table-column label="备注" prop="remark" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
    <addDialog ref="addDialogRef" @success-callback="addCallback" />
  </div>
</template>

<script setup>
  import {
    Search,
    DocumentAdd,
    Delete,
    Edit,
    Printer,
  } from '@element-plus/icons-vue'
  import { queryGoodsShelvesApi } from '@/api/wms/goods'
  import { localStorageConfig } from '@/config/localstorage.config'
  import queryDialog from './modules/queryDialog.vue'
  import addDialog from './modules/addDialog.vue'
  import hiPrint from '@/utils/hiPrint.js'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')
  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const queryForm = ref({ pkCorp: pkCorp })
  const goods = ref()

  const queryDialogRef = ref()
  const addDialogRef = ref()
  const selectedRows = ref()

  const getDate = async () => {
    let params = {
      ...paginationParams,
      ...queryForm.value,
    }
    const load = $baseLoading()
    try {
      const res = await queryGoodsShelvesApi(params)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        goods.value = res.data.list
        paginationParams.total = res.data.total
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const openQueryDialog = () => queryDialogRef.value.openDialog()

  const queryCallback = (value) => {
    queryForm.value = value
    getDate()
  }

  const openAddDialog = () => addDialogRef.value.openDialog()

  const addCallback = () => getDate()

  const handleSelectionChange = (rows) => {
    selectedRows.value = rows
  }

  const toPrint = () => {
    hiPrint('goods_001', selectedRows.value)
  }

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion

  onMounted(() => getDate())
</script>
